package com.tedu.weibo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博功能")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");
        log.debug("weiboDTO:"+weiboDTO);
        User user = (User) session.getAttribute("user");
        log.trace("通过session获得用户登录信息");
        log.debug("user:"+user);
        if (user==null){
            log.trace("user为null,未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            log.trace("user不为null,处理发表微博");
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            log.debug("weibo:"+weibo);
            log.trace("插入微博完毕");
            weiboMapper.insert(weibo);
            return JsonResult.success();
        }
    }

    @ApiOperation("查看微博首页信息")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex() throws JsonProcessingException {
        String weiboListStr = (String)redisTemplate.opsForValue().get("weiboList");
        if (weiboListStr==null){
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(weiboMapper.selectIndex());
            redisTemplate.opsForValue().set("weiboList",jsonString);
            return JsonResult.success(weiboMapper.selectIndex());
        }else {
            ObjectMapper objectMapper = new ObjectMapper();
            List<WeiboVO> list = objectMapper.readValue(weiboListStr,List.class);
            return JsonResult.success(list);
        }

    }

    @ApiOperation("获取微博详情页")
    @ApiImplicitParam(name = "id",value = "微博的ID",required = true,dataType = "int",example = "1")
    @GetMapping("/selectById")
    public JsonResult selectById(@Range(min = 1,max = 9999,message = "微博ID必须在1-9999之间") Integer id){
        return JsonResult.success(weiboMapper.selectById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "微博的ID",required = true,dataType = "int",example = "1"),
            @ApiImplicitParam(name = "name",value = "用户昵称",required = true,dataType = "String",example = "1")
    })
    @GetMapping("/selectByIdAndName")
    public WeiboDetailVO selectByIdAndName(Integer id,String name){
        return weiboMapper.selectById(id);
    }

}
